package com.ks.pc.controller.asyn;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ks.dto.BannerImgDto;
import com.ks.dto.BrandDto;
import com.ks.dto.ClassifyDto;
import com.ks.dto.OtherDto;
import com.ks.dto.PaginationDto;
import com.ks.dto.Product2Dto;
import com.ks.dto.ResultDto;
import com.ks.dto.StringifyDto;
import com.ks.dto.UserInfoDto;
import com.ks.pc.service.CommonService;
import com.ks.pc.service.SysBannerService;
import com.ks.pc.service.SysOtherService;

@Controller
@RequestMapping("/sys")
public class SysControllerAsyn {

	@Autowired
	private SysBannerService sysBannerService;
	@Autowired
	private SysOtherService sysOtherService;
	@Autowired
	private HttpServletRequest request;
	@Autowired
	private CommonService commonService;

	// 系统管理
	// =============================================
	/**
	 * 系统管理-删除不用图片
	 * 
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/delFileImgs", method = RequestMethod.POST)
	public ResultDto<String> delFileImgs() {
		ResultDto<String> result = new ResultDto<String>();
		boolean rs = sysOtherService.delFileImgs();
		result.setMsg("del fall");
		if (rs) {
			result.setMsg("del success");
		}
		return result;
	}

	// 产品世界
	// =============================================
	/**
	 * 通过条件获取分类（包括首页）
	 * 
	 * @param page
	 * @param rows
	 * @param searhName
	 *            分类名称
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/queryClassifyByCondition", method = RequestMethod.POST)
	public PaginationDto<ClassifyDto> queryClassifyByCondition(Integer page, Integer rows, String sidx, String sord,
			String searchTxt) {
		return commonService.queryClassifyByCondition(page, rows, searchTxt);
	}

	/**
	 * 通过条件获取品牌
	 * 
	 * @param classifyId
	 *            分类ID
	 * @param searchTxt
	 *            品牌名称
	 * @param page
	 * @param rows
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/queryBrandByCondition", method = RequestMethod.POST)
	public PaginationDto<BrandDto> queryBrandByCondition(Integer classifyId, String searchTxt, Integer page,
			Integer rows) {
		return commonService.queryBrandByCondition(classifyId, page, rows, searchTxt);
	}

	/**
	 * 横幅设置-获取所有图片
	 * 
	 * @param classifyId
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/banner/getClassifyImgs", method = RequestMethod.POST)
	public List<String> getClassifyImgs(Integer classifyId) {
		List<String> list = sysBannerService.getClassifyImgs(classifyId);
		return list;
	}

	/**
	 * 横幅设置-保存图片分类
	 * 
	 * @param classifyId
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/banner/saveClassifyImgs", method = RequestMethod.POST)
	public boolean save(Integer id, String imgs) {
		BannerImgDto dto = new BannerImgDto();
		dto.setClassifyId(id);
		List<String> list = new ArrayList<String>();
		for (String str : imgs.split(",")) {
			list.add(str);
		}
		dto.setList(list);
		try {
			Integer o = sysBannerService.deleteBannerImgDto(id);
			Integer n = sysBannerService.insertBannerImgDto(dto);
		} catch (Exception e) {
			return false;
		}
		return true;
	}

	// 其它设置
	// =============================================
	/**
	 * 其它设置-更新关于内容
	 * 
	 * @param dto
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/updateAbout", method = RequestMethod.POST)
	public ResultDto<String> updateAbout(OtherDto dto) {
		ResultDto<String> result = new ResultDto<String>();
		boolean rs = sysOtherService.updateAbout(dto);
		// if (rs) {
		// result.setMsg("修改成功");
		// }
		result.setType(rs);
		return result;
	}

	/**
	 * 其它设置-获取预约客户列表
	 * 
	 * @param model
	 * @param page
	 * @param rows
	 * @param searchTxt
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/findOnlineOpt", method = RequestMethod.POST)
	public PaginationDto<UserInfoDto> findOnlineOpt(Model model, Integer page, Integer rows, String searchTxt) {
		PaginationDto<UserInfoDto> dto = new PaginationDto<UserInfoDto>(page, rows);
		RowBounds rowBounds = new RowBounds(dto.getOffset(), dto.getLimit());
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("searchTxt", searchTxt);
		param.put("rowBounds", rowBounds);
		dto = sysOtherService.findOnlineOpt(dto, param);
		return dto;
	}

	/**
	 * 其它设置-预约客户列表删除操作
	 * 
	 * @param idsArr
	 *            p_online_opt表的id
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/deleteOnlineOptByIds", method = RequestMethod.POST)
	public ResultDto<String> deleteOnlineOptByIds(String idsArr) {
		ResultDto<String> result = new ResultDto<String>();
		boolean rs = sysOtherService.deleteOnlineOptByIds(idsArr);
		result.setType(rs);
		return result;
	}

	/**
	 * 其它设置-预约客户列表导出
	 * 
	 * @param yearmonth
	 * @param response
	 */
	@ResponseBody
	@RequestMapping(value = "/export")
	public void export(HttpServletRequest request, HttpServletResponse response) {
		sysOtherService.export(request, response);
	}

	/**
	 * 其它设置-更新品牌，商品排序
	 * 
	 * @param bigDto
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/updateShowIndex", method = RequestMethod.POST)
	public boolean updateShowIndex(@RequestBody StringifyDto bigDto) {
		BrandDto[] brands = bigDto.getBrands();
		Product2Dto[] products = bigDto.getProducts();
		return sysOtherService.updateShowIndex(brands, products);
	}

	// TODO del
	// =============================================
	/**
	 * 旧版login-bak.js
	 * 
	 * @param model
	 * @param username
	 * @param password
	 * @param response
	 * @throws IOException
	 */
	@RequestMapping(value = "/validate", method = RequestMethod.POST)
	public void validateView(Model model, String username, String password, ServletResponse response)
			throws IOException {

		HttpSession session = request.getSession();
		if (username.equals("admin") && password.equals("admin888")) {
			session.setAttribute("activate", true);
			String redirectUrl = "/sys/home";
			response.setCharacterEncoding("UTF-8");
			PrintWriter out = response.getWriter();
			StringBuilder json = new StringBuilder();
			json.append("{\"success\":true, \"roleIsNull\":false, ");
			json.append("\"_redirectUrl\":\"").append(redirectUrl).append("\"}");
			out.print(json.toString());
			out.flush();
			out.close();
		}
	}
}
